<?php
class tejia_orderControl extends skymvc{
	
	public function __construct(){
		parent::__construct();	
	}
	
	public function onInit(){
		M("login")->checkLogin();
		$this->loadConfig("table"); 
	}
	public function onDefault(){
		
	}
	
	public function onBuy(){
		$userid=M("login")->userid;
		$id=get_post('id','i');
		$data=M("tejia")->selectRow("id=".$id);
		if($data['endtime']<time()){
			$this->goAll("特价活动已结束",1);
		}
		if($data['starttime']>time()){
			$this->goAll("特价活动还未开始",1);
		}
		$uo=M("tejia_order")->selectRow(array("where"=>"object_id=".$id." AND userid=".$userid,"order"=>"order_id DESC"));
		if($uo){
			if($data['typeid']!=2 || date("Ymd",$uo['dateline'])==date("Ymd")){
				$this->goALl("你已经购买过了",1);
			}
		}
		$address=M("user_address")->select(array("where"=>" siteid=".SITEID." AND status<10 AND userid=".M("login")->userid,"shop_order"=>"isdefault desc","limit"=>1));
		if($address){
			foreach($address as $v){
				$d_ids[]=$v['province_id'];
				$d_ids[]=$v['city_id'];
				$d_ids[]=$v['town_id'];
			}
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
		}
		$this->smarty->goassign(array(
			"address"=>$address,
			"dist_list"=>$dist_list,
			"back_url"=>$_SERVER['HTTP_REFERER'],
			"data"=>$data,
			"pay_type_list"=>pay_type_list(),
		));
		$tpl=M("pagetpl")->get("tejia_order","buy");
		$this->smarty->display($tpl);
		$this->smarty->display("tejia_order/buy.html");
	}
	
	public function onBuySave(){
	  	$userid=M("login")->userid;
		$id=get_post('id','i');
		$amount=max(1,get_post('amount','i'));
		$data=M("tejia")->selectRow("id=".$id);
		if($data['endtime']<time()){
			$this->goAll("特价活动已结束",1);
		}
		if($data['starttime']>time()){
			$this->goAll("特价活动还未开始",1);
		}
		if($data['limit_num']<$amount){
			$this->goALl("最多只能购买".$data['limit_num']."份");
		}
		$shop=M("shop")->selectRow("shopid=".$data['shopid']);
		$uo=M("tejia_order")->selectRow(array("where"=>"object_id=".$id." AND userid=".$userid,"order"=>"order_id DESC"));
		if($uo){
			if($data['typeid']!=2 || date("Ymd",$uo['dateline'])==date("Ymd")){
				$this->goALl("你已经购买过了".$data['typeid'],1);
			}
		}
		$user_address_id=post('user_address_id','i');
		$addr=M('user_address')->selectRow(array("where"=>"id=".$user_address_id));
		if(empty($addr) or empty($addr['telephone'])){
			$this->goall("请完善收货地址",1);
		}
		$total_money=$amount*$data['price'];
		$unpay=0;
		$ispay=1;
		$pay_type=get_post('pay_type','h');
		if($pay_type=='moneypay'){
			if($total_money>$user['money']){
				$this->goALL("余额不足",1);
			}else{
				//添加金钱消费记录		
				M("user")->addMoney(array(
					"userid"=>M("login")->userid,
					"money"=>-$total_money,
					"content"=>"您购买了产品花了[money]元",	
				));
				$ispay=2;
				$isfinish=1;
			}
		}
		 
		
		if($pay_type=='unpay'){
			$isfinish=1;
			$unpay=1;
		}
		$orderno="tj".date("YmdHis").$userid;
		$order_id=M("tejia_order")->insert(array(
				"orderno"=>$orderno,
				"dateline"=>time(),
				"ispay"=>$ispay,
				"userid"=>$userid,
				"money"=>$total_money,
				"user_address_id"=>$user_address_id,
				"object_id"=>$id,
				"unpay"=>$unpay,
				"shopid"=>$data['shopid'],
				"tablename"=>$data['tablename'],
				"sushe"=>$addr['sushe'],
				"sc_id"=>$addr['city_id'],
				"paytype"=>$pay_type,
				"siteid"=>SITEID
		));
		M("tejia_order_product")->insert(array(
						"order_id"=>$order_id,
						"userid"=>$userid,
						"object_id"=>$data['id'],
						"price"=>$data['price'],
						"amount"=>$amount,
						"dateline"=>time(),
		));
		//处理订单收货地址
			$d_ids=array($addr['province_id'],$addr['city_id'],$addr['town_id']);
			$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
			M("tejia_order_address")->insert(array(
				"order_id"=>$order_id,
				"userid"=>$userid,
				"truename"=>$addr['truename'],
				"telephone"=>$addr['telephone'],
				"p_c_t"=>$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']],
				"address"=>$addr['address'],
				"dateline"=>time(),
				"sushe"=>$addr['sushe']
			));
			//打印收货地址
			
			if($shop['sendall']){
				$address=$addr['address'];
			}else{
				$address=$dist_list[$addr['province_id']].$dist_list[$addr['city_id']].$dist_list[$addr['town_id']];
			}
		//发送推送信息
		$shopid=$data['shopid'];
		$prostr=$data['title'] ."x".$amount;
		$content=date("H:i:s")."特价新订单,详情".$prostr; 
		M("apppush")->sendShop(array(
				"table"=>"shop",
				"content"=>$content,
				"shopid"=>$shopid,
		));
		//End 推送信息
		//手机短信
		if(SMS_SHOP_ORDER_NEW){
			$smsdata=array(
				"key"=>"order",
				"para"=>array($orderno."(学生街特价订单 ".date("Y-m-d H:i:s").")","",""),
				"content"=>"【".SMS_QIANMING."】".$content
			);
			$shop_admins=M("shopadmin")->select(array("where"=>" shopid=".$shopid));
			if($shop_admins){
				 
				foreach($shop_admins as $y){
					if(is_tel($y['telephone'])){
						M("smspush")->insert(array(
							"telephone"=>$y['telephone'],
							"dateline"=>time(),
							"content"=>base64_encode(serialize($smsdata))
						));
					}
				}
			}
		}
		$data=array(
			"order_id"=>$order_id,
			"pay_type"=>$pay_type,
			"ispay"=>$ispay
		);
		
		/****打印服务****/
			//打印产品信息
			if(HAS_PRINTER==1){
			$printer_products=array();
			$printer_products[]=array(
				"title"=>$data['title'],
				"price"=>$data['price'],
				"amount"=>$amount,
				"money"=>$data['price']*$amount,
			);
			if($isfinish){
				$printer_isvalid=1;
			}else{
				$printer_isvalid=0;
			}
			$pays=pay_type_list(1);
			$printer_order=array(
				"money"=>$total_money,
				"dateline"=>time(),
				"pay_type"=>$pays[$pay_type]
			);
			$printer_address=array(
				"address"=>$address."宿舍".$addr['sushe'],
				"telephone"=>$addr['telephone'],
				"nickname"=>$addr['truename']
			);
			$printer=M("mod_printer")->selectRow("tablename='shop' AND bstatus=2 AND shopid=".$shopid);
			$printer_content=array(
				"products"=>$printer_products,
				"addr"=>$printer_address,
				"order"=>$printer_order,
			);
			$printer_content=base64_encode(json_encode($printer_content));
			if($printer ){
				M("mod_printer_plan")->insert(array(
					"tablename"=>"ymdian",
					"shopid"=>$shopid,
					"title"=>"订单".$orderno,
					"dateline"=>time(),
					"last_time"=>time(),
					"ftable"=>"tejia_order",
					"fid"=>$order_id,
					"content"=>$printer_content,
					"isvalid"=>$printer_isvalid
				));
			}
			}
	/*****End打印服务**********/
			
		if($isfinish){
			$this->goall("恭喜你，抢购成功",0,$data,"/index.php?m=tejia_order&a=show&order_id=".$order_id);
		}else{
			$this->goall("恭喜你，抢购成功，快去支付吧",0,$data,"/index.php?m=tejia_order&a=pay&pay_type=".$pay_type."&order_id=".$order_id);
		}
	}
	
	public function onPay(){
		$order_id=get('order_id','i');
		$pay_type=get('pay_type','h');
		header("Location:  /index.php?m=recharge&a=default&table=tejia&pay_type=".$pay_type."&order_id=".$order_id);
		exit();
		$this->smarty->assign(array(
			"payurl"=>"/index.php?m=recharge&a=default&table=tejia&pay_type=".$pay_type."&order_id=".$order_id,
			"order"=>M("tejia_order")->selectRow("order_id=".$order_id)
		));
		//$this->smarty->display("ymdian_order/pay.html");
		header("Location: /index.php?m=recharge&a=default&table=tejia&pay_type=".$pay_type."&order_id=".$order_id);
	}
	
	 
	
	public function onMy(){
		$where="  userid=".M("login")->userid;
		$url=APPINDEX."?m=tejia_order&a=my";
		$start=get('per_page','i');
		$limit=20;
		$status=get('status','i');
		$where.=" AND status=".$status;
		$url.="&status=".$status;
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("tejia_order")->select($option,$rscount);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("tejia_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"shop_order"=>"id DESC"));
			 
			 	$v['shop']=M("shop")->selectRow("shopid=".$v['shopid']);
				$v['products']=$this->orderproduct($v['order_id']);
				$v['num']=M("tejia_order_product")->selectOne(array("where"=>"order_id=".$v['order_id'],"fields"=>"sum(amount) as ct")); 
				$v['status_name']=$order_status_list[$v['status']];
				if($v['paytype']!='unpay'){
					$v['ispay_name']=$order_ispay[$v['ispay']];
				}else{
					$v['ispay_name']="货到付款";
				}
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		
		$this->smarty->goassign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$tpl=M("pagetpl")->get("tejia_order","my");
		$this->smarty->display($tpl);
		$this->smarty->display("tejia_order/my.html");
	}
	

	
	public function onshow(){
		 
		$order_id=get('order_id','i');
		$data=M("tejia_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['userid']!=M("login")->userid){
			$this->goall("该订单不是您的",1);
		}
		if($data['tablename']=='shop'){
			$shop=M("shop")->selectRow("shopid=".$data['shopid']);
		}
		$addr=M("tejia_order_address")->selectRow(array("where"=>"order_id=".$order_id,"order"=>"id DESC"));
		 
		$shop_order_status_list=$this->config_item('order_status_list');
		
		$shop_order_type_list=$this->config_item('order_type_list');
		$shop_order_ispay=$this->config_item('order_ispay');
		//获取商品
		 
		$shop_order_product=M("tejia_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		$total_num=0;
		foreach($shop_order_product as $k=>$v){
			$p=M("tejia")->selectRow(array("where"=>"id=".$v['object_id']));
			$p['order_price']=$v['price'];
			$p['amount']=$v['amount'];
			$total_num+=$v['amount']; 
			$shop_order_product[$k]=$p;
		}
		 
		$this->smarty->goassign(array(
			"data"=>$data,
			"addr"=>$addr,
			"order_status_list"=>$shop_order_status_list,
			"order_type_list"=>$shop_order_type_list,
			"order_ispay"=>$shop_order_ispay,
			"order_product"=>$shop_order_product,
			"shop"=>$shop,
			"total_num"=>$total_num,
		));
		$tpl=M("pagetpl")->get("tejia_order","show");
		$this->smarty->display($tpl);
		$this->smarty->display("tejia_order/show.html");
	}
	
	public function orderproduct($order_id){
		
		$shop_order_product=M("tejia_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		foreach($shop_order_product as $k=>$v){
			$p=M("tejia")->selectRow(array("where"=>"id=".$v['object_id']));
			$p['shop_order_price']=$v['price'];
			$p['amount']=$v['amount'];
			$shop_order_product[$k]=$p;
		}
		return $shop_order_product;	
	}
	
	public function onCancel(){
		
		$order_id=get_post('order_id','i');
		$data=M("tejia_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['status']>=3) $this->goall("操作失败",1);
		if($data['userid']!=M("login")->userid){
			$this->goall("该订单不是您的",1);
		}
		M("tejia_order")->begin();
		M("tejia_order")->update(array("status"=>10),"order_id=".$order_id);
		$content=post('content');
		$message=post('message','h');
		M("tejia_order_log")->insert(array(
			"dateline"=>time(),
			"order_id"=>$order_id,
			"content"=>$content."，原因：".$message,
		));
		//加商品库存
		$order_product=M("tejia_order_product")->select(array("where"=>"order_id=".$order_id));
		foreach($order_product as $v){
		 
			M("tejia")->total_num_add(array(
				"shopid"=>$data['shopid'],
				"id"=>$v['object_id'],
				"total_num"=>$v['amount'],
				"content"=>"[下单取消]"
			));
			
		}
	 
		//退钱给用户
		if($data['ispay']==2){
			/*$row=M("recharge_back")->selectRow(" order_id=".$order_id." AND tablename='tejia' ");
			if(empty($row)){
				M("recharge_back")->insert(array(
					"order_id"=>$order_id,
					"tablename"=>"tejia",
					"paytype"=>$data['paytype'],
					"dateline"=>time(),
					"ip"=>ip(),
					"userid"=>M("login")->userid	
				));
			}
			**/
			$this->loadControl("jfapi","source/index/");
			$user=M("user")->selectRow(array("where"=>"userid=".$data['userid']));
			$this->jfapiControl->setUserId($data['userid'])->addMoney(array(
						"money"=>$data['money'],
						"type_id"=>1,
						"ispay"=>2,
						"content"=>"您的订单被取消了，退还给您￥".$data['money']."，之前有￥[oldmoney]，目前还剩￥[newmoney]",
			));
			
		}
		M("shop_order")->commit();
		$this->goall("取消成功");
	}
	
	
}

?>